atan2 speed unity

62

atan2 speed unity -

Vector2[] directions = // populate this with your 16 directions.
                       // Counter-clockwise from (1, 0)

Vector2 RoundToNearestDirection(Vector2 vector) {
     // Convert the input vector to an angle.
     float angle = Mathf.Atan2(vector.y, vector.x);
     // Round to one of 16 buckets, using modulo to wrap 16 back to 0.
     int index = Mathf.Round((angle * 8 / Mathf.PI) + 16) % 16;
     // Look up the corresponding entry in our direction table.
     return directions[index];
}

Comments

Submit
0 Comments